home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Crossword / Source / BackjumpSquare.m < prev    next >
Text File  |  1995-06-12  |  2KB  |  94 lines

  1. /*
  2.  
  3. File BackjumpSquare.m
  4.  
  5. These are the squares used for backjumping search.  When a letter has no remaining possibilities, the words that intersect at that letter are added to the current nogood.  The current nogood is cleared whenever a letter is successfully filled.
  6.  
  7. */
  8.  
  9. #import <appkit/appkit.h>
  10.  
  11. #import "Backjump.h"
  12. #import "Puzzle.h"
  13.  
  14.  
  15. /* ————————————————————————————————————————————————————————————————————————————  */
  16.  
  17.  
  18. #define position(i)        ((wordPosition *) [words  elementAt: i])
  19.  
  20.  
  21. /* ————————————————————————————————————————————————————————————————————————————  */
  22.  
  23.  
  24. @implementation BackjumpSquare
  25.  
  26.  
  27. - startOver
  28. {
  29.     id        state;
  30.     
  31.     if ([state = [puzzle  getState]  inNogood]) [state  isok];
  32.     return self;
  33. }
  34.  
  35.  
  36. - erase
  37. {
  38.     [self  chooseLetter];
  39.     [[[self  clear]  setLetter: WILDCARD]  show];
  40.     
  41.     return self;
  42. }
  43.  
  44.  
  45. - clear
  46. {
  47.     id        state;
  48.     int        i;
  49.     
  50.     if (!injump)
  51.     {
  52.         state = [puzzle  getState];
  53.         i = [words  count];
  54.         while (i--) [state  mustChange: position(i)->word];
  55.     }
  56.     
  57.     return [super  clear];
  58. }
  59.  
  60.  
  61. - setLetter: (char) theLetter
  62. {
  63.     [super  setLetter: theLetter];
  64.     if (letter != WILDCARD) [[puzzle  getState]  isok];
  65.             
  66.     return self;
  67. }
  68.  
  69.  
  70. - (char) chooseLetter
  71. {
  72.     char    best;
  73.     int        i;
  74.     
  75.     if (![[puzzle  getState]  inNogood]) injump = NO;
  76.     else
  77.     {
  78.         injump = YES;
  79.         i = [words  count];
  80.         while (i--) injump &= ![position(i)->word  mustChange];
  81.     }
  82.         
  83.     if (!injump) best = [super  chooseLetter];
  84.     else best = WILDCARD;
  85.     
  86.     if (injump)
  87.             [self  setStatus: JUMPED];        else
  88.             [self  clearStatus: JUMPED];
  89.     
  90.     return best;
  91. }
  92.  
  93.  
  94. @end